package com.aca.market.controller;


import com.aca.market.entity.Permission;
import com.aca.market.entity.Role;
import com.aca.market.entity.RolePerm;
import com.aca.market.service.IPermissionService;
import com.aca.market.service.IRolePermService;
import com.aca.market.service.IRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aca
 * @since 2020-11-13
 */
@Controller
@Slf4j
public class RolePermController {

//    private static final Long nowRoleId = 0L;

    @Autowired
    IRolePermService rolePermService;
    @Autowired
    IPermissionService permissionService;
    @Autowired
    IRoleService roleService;

    @GetMapping("/role/list")
    public String toRole(Model model){
        List<Role> roles = roleService.list();
        model.addAttribute("roles",roles);
        return "role-list";
    }

    @GetMapping("/role/detail")
    public String roleDetail(@RequestParam Long roleId,Model model){
        Role role = roleService.getById(roleId);
        //拿到所有的权限和该用户拥有的权限id
        List<Long> permId = permissionService.getPermsId(roleId);
        List<Permission> allPerm = permissionService.list();
//        restPerm.removeAll(permission);
        model.addAttribute("allPerm",allPerm);
        model.addAttribute("permId",permId);
        model.addAttribute("role",role);
        return "role-detail";
    }

    @GetMapping("/role/update")
    public String roleUpdate(Role role){
        if (role.getId()!=99L){
            roleService.updateById(role);
        }
        return "redirect:/role/list";
    }

    @GetMapping("/role/del")
    public String roleDel(@RequestParam Long roleId){
        //不准动超管
        if (roleId==99L) return "/no-auth";
        //可能无权限 不和下面并一起了 下面同理
        rolePermService.remove(new QueryWrapper<RolePerm>()
                .eq("role_id",roleId));
        roleService.removeById(roleId);
        return "redirect:/role/list";
    }

    @PostMapping("/role/delete/{roleIds}")
    @ResponseBody
    public boolean rolesDel(@PathVariable List<Long> roleIds){
        if (roleIds.contains(99L)) return false;
        rolePermService.remove(new QueryWrapper<RolePerm>()
                .in("role_id",roleIds));
        return roleService.removeByIds(roleIds);
    }

    @GetMapping("/role/toAdd")
    public String toRoleAdd(Model model){
        List<Permission> allPerm = permissionService.list();
        model.addAttribute("allPerm",allPerm);
        return "role-add";
    }


    @PostMapping("/role/add")
    @ResponseBody
    public Long roleAdd(@RequestBody Role role){
        Role tmp = role;
        Long nowRoleId = 0L;
        if (roleService.save(tmp)){
            //拿到自增主键
            nowRoleId = tmp.getId();
        }
        return nowRoleId;
    }


    @PostMapping("/perm/add/{roleId}/{permIdList}")
    @ResponseBody
    public boolean permListAdd(@PathVariable Long roleId,@PathVariable List<Long> permIdList){
        List<RolePerm> rolePerms = roleService.convertToRolePerm(permIdList, roleId);
        return rolePermService.saveBatch(rolePerms);
    }

    @PostMapping("/perm/del")
    @ResponseBody
    public boolean permDel(@RequestParam Long permId,@RequestParam Long roleId){
        return rolePermService.remove(new QueryWrapper<RolePerm>()
                .eq("role_id",roleId)
                .eq("permission_id",permId));
    }

    @PostMapping("/perm/add")
    @ResponseBody
    public boolean permAdd(@RequestParam Long permId,@RequestParam Long roleId){
        return rolePermService.save(new RolePerm()
                .setPermissionId(permId)
                .setRoleId(roleId));
    }
}

